package com.webserver.http;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class HttpContext {
    private static Map<String,String>mimeMapping = new HashMap<>();

    static {
        initMineMapping();
    }

    private static void initMineMapping(){
        try {
            SAXReader reader = new SAXReader();
            Document doc = reader.read("./conf/web.xml");
            Element root = doc.getRootElement();
            List<Element>list = root.elements("mime-mapping");
            for (Element e : list ){
                String key = e.elementTextTrim("extension");
                String value = e.elementTextTrim("mime-type");
                mimeMapping.put(key,value);
            }
        } catch (DocumentException e) {
            e.printStackTrace();
        }

    }

    public static String  getMimeType(String key){
        return mimeMapping.get(key);
    }

//    public static void main(String[] args) {
//        System.out.println(mimeMapping.size());
//    }
}
